package ui.widget;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BoxLayout;
import javax.swing.JPanel;

import ui.other.YPBorderFactory;
import ui.other.YPPanelClicked;
import ui.util.YPUIUtil;

public class ColorXTransparentPanel extends JPanel{
	private int height;
	private boolean isEnter;
	private boolean isSelected;
	private Color mouseInColor;
	private YPPanelClicked clickInteface;
	public ColorXTransparentPanel() {
		init();
	}
	public ColorXTransparentPanel(int w,int h) {
		height=h;
		setPreferredSize(new Dimension(w, h));
		setMaximumSize(new Dimension(YPUIUtil.getScreenWidth(), h));
//		setSize(new Dimension(w, h));
		init();
		
	}
	private void init() {
		isEnter=false;
		isSelected=false;
//		mouseInColor = new Color(209, 209, 209);
		mouseInColor =Color.BLUE;
		clickInteface=null;
		setBorder(YPBorderFactory.getUnderLineBorder());
		setOpaque(false);
		setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		addMouseListener(new MouseAdapter() {
			@Override
			public void mouseEntered(MouseEvent e) {
				isEnter = true;
				setCursor(new Cursor(Cursor.HAND_CURSOR));
				repaint();
			}

			@Override
			public void mouseExited(MouseEvent e) {
				isEnter = false;
				repaint();
			}

			@Override
			public void mouseClicked(MouseEvent e) {
				if(clickInteface!=null) {
					clickInteface.click(e);
				}
			}
			
		});
	}
	
	public void setPanelClickedEvent(YPPanelClicked clickInteface) {
		this.clickInteface = clickInteface;
	}
	public YPPanelClicked getPanelClickedEvent() {
		return this.clickInteface;
	}
	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public boolean isEnter() {
		return isEnter;
	}

	public void setEnter(boolean isEnter) {
		this.isEnter = isEnter;
		repaint();
	}

	public boolean isSelected() {
		return isSelected;
	}

	public void setSelected(boolean isSelected) {
		this.isSelected = isSelected;
		repaint();
	}

	public Color getMouseInColor() {
		return mouseInColor;
	}

	public void setMouseInColor(Color mouseInColor) {
		this.mouseInColor = mouseInColor;
		repaint();
	}

	@Override
	protected void paintComponent(Graphics g) {
		Graphics2D g2d = (Graphics2D) g.create();
		int h = getHeight();
		int w = getWidth();
		float tran = 0f;
		if (isEnter) {
			tran = 0.3f;
		}
		if (isSelected) {
			tran = 0.5f;
		}
		g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, tran));
		g2d.setColor(mouseInColor);
		g2d.fillRect(0, 0, w, h);
		childRepaintAfterMe(g);
		g2d.dispose();
	}
	
	protected void childRepaintAfterMe(Graphics g) {
		
	}
}
